package com.warrior.main;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.androidWarrior.R;
import com.facebook.Session;
import com.warrior.games.activitys.GameActivity;
import com.warrior.main.MyApp.IBluetoothChanged;
import com.warrior.main.MyFacebook.ISessionState;

public class MyActivity extends Activity implements IBluetoothChanged, ISessionState{
	private final static int ABOUT_ACTIVITY = 0,HELP_ACTIVITY = 1,LOG_IN_FACEBOOK = 2;
	protected static final int REQUEST_CODE_FACEBOOK_LOGIN = 64206;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyApp app = (MyApp)getApplication();
		app.getFacebook().setStateLoginListener(this);
		
	}
	@Override
	protected void onResume() {
		super.onResume();
		MyApp app = (MyApp)getApplication();
		// connection has closed return to MainActivity 
		if(!app.isConnectedWithOtherDevice() && 
				((this instanceof SelectGameActivity) || (this instanceof GameActivity))){
			finish();
		}
		app.setBluetoothChangedListener(this);
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode){
	    	case REQUEST_CODE_FACEBOOK_LOGIN:{
	    		if(resultCode == -1){ // -1 has log in successfully
	    			Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
	    		}
				break;
			}
		}
	}
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MyApp app = (MyApp)getApplication();
		MenuItem facebookItem = menu.getItem(LOG_IN_FACEBOOK);
	   	if(app.getFacebook().isOpenedConnection()){
	   	
	   		facebookItem.setTitle(getResources().getString(R.string.facebook_logout));
	   	}
	   	else{
	   		facebookItem.setTitle(getResources().getString(R.string.facebook_login));
	   	}
		return super.onPrepareOptionsMenu(menu);
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		// create the optionMenu
	   	MenuInflater flate = getMenuInflater();
	   	flate.inflate(R.menu.option_menu, menu);
	   	return super.onCreateOptionsMenu(menu);
	}
	public boolean onOptionsItemSelected(MenuItem item) {
		int order = item.getOrder();
	   	switch(order){
	    	case ABOUT_ACTIVITY:{
	    		Intent iAboutActivity = new Intent(this,AboutActivity.class);
		    	startActivity(iAboutActivity);
		   		break;
		   	}
		   	case HELP_ACTIVITY:{
		   		Intent iHelpActivity = new Intent(this,HelpActivity.class);
	    		startActivity(iHelpActivity);
	    		break;
		    }
			case LOG_IN_FACEBOOK:{
				MyApp app = (MyApp)getApplication();
				if(app.getFacebook().isReadyOpenConnection()){
			    	app.getFacebook().openSession(this);
				}
				else if(app.getFacebook().isOpenedConnection()){
			   		app.getFacebook().closeSession();
			   	}
	    		break;
		    }
	    }
	   	return super.onOptionsItemSelected(item);
	}
	public void bluetoothStateConnectionChanged(boolean isConnected,String message) {
		if(!isConnected){
			MyApp app = (MyApp)getApplication();
			app.getFacebook().resetDataCompetitor();
			finish();
		}
	}
	@Override
	public void bluetoothStateChanged(int state) {
		switch (state) {
		case BluetoothAdapter.STATE_TURNING_OFF:
			bluetoothStateConnectionChanged(false,"connection close");
			break;

		case BluetoothAdapter.STATE_OFF:
			bluetoothStateConnectionChanged(false,"connection close");
			break;
		}
	}
	@Override
	public void facebookOpenSession(boolean isSuccess, String exception) {
		if(isSuccess){
			MyApp app = (MyApp)getApplication();
			MyFacebook fb =app.getFacebook(); 
			// add new permission for publish post on wall 
			if(!fb.isFoundPermission(MyFacebook.PUBLISH_PERMISSION)){
				fb.addPermission(this, MyFacebook.PUBLISH_PERMISSION);
			}
		}
		else{
			Toast.makeText(this, exception, Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public void facebookCloseSession() {
		
	}	
	protected void showToast(String text){
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}

}
